package com.hsc.offer;

/**
 * @author
 * @date 2025-06-29
 * @Description 18.操作给定的二叉树，将其变换为源二叉树的镜像。
 * <p>
 * 其实就是将二叉树树的的左右节点进行交换
 */
public class OfferTest13 {
    public static void main(String[] args) {
        // 创建一个二叉树
        TreeNode treeNode8 = new TreeNode(8);
        TreeNode treeNode9 = new TreeNode(9);
        TreeNode treeNod10 = new TreeNode(10);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        TreeNode treeNode7 = new TreeNode(7);
        TreeNode treeNode11 = new TreeNode(11);

        // 构建二叉树
        treeNode8.left = treeNode6;
        treeNode8.right = treeNod10;
        treeNode6.left = treeNode5;
        treeNode6.right = treeNode7;
        treeNod10.left = treeNode9;
        treeNod10.right = treeNode11;
        Mirror(treeNode8);
    }


    public static void Mirror(TreeNode root) {
        if (root == null) return;
        // 使用中间变量进行交换
        TreeNode temp = root.left;
        System.out.println("temp="+temp.val);
        root.left = root.right;
        root.right = temp;
        System.out.println("root.left="+root.left);
        System.out.println("root.right="+root.right);
        Mirror(root.left);
        Mirror(root.right);
    }
}

